﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace HelloASPX
{
    public partial class UpdateUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                HttpContext.Current.Response.Write("<script>alert('请先登陆！');location='Login.aspx'</script>");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (username.Text == "")
            {
                return;
            }
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/user.xml"));
            XmlNode usernode = doc.SelectSingleNode("/users/user[username='" + username.Text + "']");
            if (usernode.SelectSingleNode("mima").InnerText == oldPassword.Text)
            {
                usernode.SelectSingleNode("mima").InnerText = newPassword.Text;
                doc.Save(Server.MapPath("~/App_Data/user.xml"));
                Response.Write("<script>alert('修改密码成功。');;location='Login.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('修改失败，旧密码错误！');</script>");
            }
        }

        protected void passwordQuestion_TextChanged(object sender, EventArgs e)
        {

        }

        protected void username_TextChanged(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/user.xml"));
            XmlNode usernode = doc.SelectSingleNode("//user[username='" + username.Text + "']");
            if (usernode==null)
            {
                usernameLabel.Text = "该用户名不存在！仔细看看是不是填错了！";
            }
            else
            {
                Panel1.Visible = true;
                usernameLabel.Text = "";
                passwordQuestion.Text = usernode.SelectSingleNode("passwordQuestion").InnerText;
            }
        }

        protected void answer_TextChanged(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/user.xml"));
            XmlNode usernode = doc.SelectSingleNode("//user[username='" + username.Text + "']");
            if (usernode.ChildNodes[3].InnerText == answer.Text)
            {
                Panel2.Visible = true;
            }
            else
            {
                answerLabel.Text = "对不起，答案错误！";
                answer.Text = "";
                answer.Focus();
            }
        }
    }
}